package com.ozo.easy.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ozo.easy.common.auth.UserLoginHelper;
import com.ozo.easy.entity.SysFile;
import com.ozo.easy.entity.SysLog;
import com.ozo.easy.pojo.vo.WelcomeCountVO;
import com.ozo.easy.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 页面控制器
 *
 * @author ozo
 * @date 2024-12-20
 */
@RequiredArgsConstructor
@Tag(name = "页面跳转")
@Controller
public class PageController {

    private final ISysMenuService sysMenuService;

    private final ISysNoticeService sysNoticeService;

    private final ISysUserService sysUserService;

    private final ISysFileService sysFileService;

    private final ISysLogService sysLogService;

    @Operation(summary = "首页")
    @GetMapping({"/index", "/"})
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userInfo", sysUserService.detail(UserLoginHelper.getUserId()));
        modelAndView.addObject("menuList", sysMenuService.listAuth());
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @Operation(summary = "欢迎页")
    @GetMapping({"/welcome"})
    public ModelAndView welcome() {
        ModelAndView modelAndView = new ModelAndView();

        WelcomeCountVO welcomeCountVO = new WelcomeCountVO();
        welcomeCountVO.setUserNum(sysUserService.count());
        welcomeCountVO.setOnlineUserNum((long) StpUtil.searchTokenValue(null, 0, -1, true).size());
        welcomeCountVO.setFileNum(sysFileService.count());
        welcomeCountVO.setAvatarFileNum(sysFileService.lambdaQuery().likeRight(SysFile::getFilePath, "avatar").count());
        welcomeCountVO.setLogNum(sysLogService.count());
        welcomeCountVO.setFailLogNum(sysLogService.lambdaQuery().eq(SysLog::getSuccessFlag, false).count());
        modelAndView.addObject("count", welcomeCountVO);


        LocalDate ago = LocalDate.now().minusDays(6);
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DATE_FORMAT(operation_time,'%Y-%m-%d') as days, count(id) as num")
                .eq("request_url", "/auth/login")
                .ge("operation_time", ago.atStartOfDay())
                .groupBy("days");
        List<Map<String, Object>> maps = sysLogService.listMaps(queryWrapper);
        List<String> days = new ArrayList<>();
        List<Long> nums = new ArrayList<>();
        while (!ago.isAfter(LocalDate.now())) {
            days.add(ago.toString());
            Long num = 0L;
            for (Map<String, Object> map : maps) {
                String days1 = (String) map.get("days");
                if (ago.toString().equals(days1)) {
                    num = (Long) map.get("num");
                }
            }
            nums.add(num);
            ago = ago.plusDays(1);
        }
        JSONObject result = new JSONObject();
        result.put("days", days);
        result.put("nums", nums);
        modelAndView.addObject("tjt", result);

        modelAndView.addObject("noticeList", sysNoticeService.listNew(8));
        modelAndView.setViewName("welcome");
        return modelAndView;
    }

    @SaIgnore
    @Operation(summary = "登录")
    @GetMapping("/login")
    public String toLogin() {
        return "login";
    }

    @SaIgnore
    @Operation(summary = "注册")
    @GetMapping("/register")
    public String toRegister() {
        return "register";
    }

    @SaIgnore
    @Operation(summary = "403页面")
    @GetMapping("/error/403")
    public String error403() {
        return "error/403";
    }

    @SaIgnore
    @Operation(summary = "404页面")
    @GetMapping("/error/404")
    public String error404() {
        return "error/404";
    }

    @SaIgnore
    @Operation(summary = "500页面")
    @GetMapping("/error/500")
    public String error500() {
        return "error/500";
    }

}
